/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 6/3/2009
 * Time: 12:19 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace MapSharpLib
{
	[Serializable]
	public class Job : ISerializable
	{
		public string JobName;
		public byte[] Assembly;
		public string MapReducerClass;
		public IEnumerable<ISerializable> Inputs;
		public string Client;
		public ISerializable Results;
		public bool isDone;
		private Dictionary<string, string> otherAttributes;
		
		private Job()
		{}
		
		public Job(string JobName, string Client, byte[] Assembly, string MapReducerClass, IEnumerable<ISerializable> Inputs)
		{
			this.JobName = JobName;
			this.Assembly = Assembly;
			this.Client=Client;
			this.MapReducerClass = MapReducerClass;
			this.Inputs = Inputs;
			this.isDone = false;
		}
		
		public void SetResults(ISerializable results)
		{
			this.Results = results;
			Inputs = null;
			this.isDone = true;
		}
		
		public void setAtt(string attName, string value)
		{
			if(otherAttributes==null)
			{
				otherAttributes = new Dictionary<string, string>();
			}
			otherAttributes[attName]=value;
		}
		
		public string getAtt(string attName)
		{
			string retVal = null;
			if(otherAttributes!=null && otherAttributes.TryGetValue(attName, out retVal))
			{
				return retVal;
			}
			return string.Empty;			
		}
		
		public Job Clone()
		{
			Job j = new Job();
			j.JobName = this.JobName;
			j.Assembly = this.Assembly;
			j.MapReducerClass = this.MapReducerClass;
			j.Inputs = this.Inputs;
			j.Client = this.Client;
			j.Results = this.Results;
			j.isDone = this.isDone;
			j.otherAttributes = this.otherAttributes;
			return j;
		}
		
		#region Serialization stuff		
		protected Job (SerializationInfo info, StreamingContext c) 
        {
			this.JobName = info.GetString("JobName");
			this.Assembly = (byte[])info.GetValue("Assembly", typeof(byte[]));
			this.Client = info.GetString("Client");
			this.MapReducerClass = info.GetString("MapReducerClass");
			this.isDone = info.GetBoolean("isDone");
			this.otherAttributes = (Dictionary<string, string>)info.GetValue("otherAttributes", typeof(Dictionary<string, string>));
			if(isDone)
				this.Results = (ISerializable) info.GetValue ("Results", typeof(ISerializable));
			else
			{
				this.Inputs = (IEnumerable<ISerializable>) info.GetValue ("Inputs", typeof(IEnumerable<ISerializable>));
			}
        }
		
		[SecurityPermissionAttribute(SecurityAction.LinkDemand,SerializationFormatter=true)]
        public virtual void GetObjectData (SerializationInfo info, StreamingContext context) 
        {
        	info.AddValue ("JobName", JobName);
			info.AddValue ("Assembly", Assembly);
			info.AddValue ("Client", Client);
			info.AddValue ("isDone", isDone);
			info.AddValue ("MapReducerClass", MapReducerClass);
			info.AddValue ("otherAttributes", otherAttributes);
			if(isDone)
				info.AddValue ("Results", Results);
			else
			{
				info.AddValue("Inputs", Inputs);
			}
        }
        #endregion
	}
}
